home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Source Code / Visual Basic Source Code.iso / vbsource / puzzle / puzzle.frm (.txt) < prev    next >
Encoding:
Visual Basic Form  |  1999-09-07  |  23.5 KB  |  645 lines

  1. VERSION 5.00
  2. Begin VB.Form Puzzle 
  3.    AutoRedraw      =   -1  'True
  4.    Caption         =   "Puzzle"
  5.    ClientHeight    =   7068
  6.    ClientLeft      =   1272
  7.    ClientTop       =   1620
  8.    ClientWidth     =   8268
  9.    LinkTopic       =   "Form1"
  10.    ScaleHeight     =   7068
  11.    ScaleWidth      =   8268
  12.    Begin VB.Frame Frame1 
  13.       Height          =   4092
  14.       Left            =   1320
  15.       TabIndex        =   9
  16.       Top             =   1200
  17.       Width           =   3972
  18.       Begin VB.CommandButton Command1 
  19.          Caption         =   "8"
  20.          BeginProperty Font 
  21.             Name            =   "MS Sans Serif"
  22.             Size            =   18
  23.             Charset         =   0
  24.             Weight          =   700
  25.             Underline       =   0   'False
  26.             Italic          =   0   'False
  27.             Strikethrough   =   0   'False
  28.          EndProperty
  29.          Height          =   1332
  30.          Index           =   7
  31.          Left            =   2640
  32.          TabIndex        =   17
  33.          Top             =   2760
  34.          Width           =   1332
  35.       End
  36.       Begin VB.CommandButton Command1 
  37.          Caption         =   "7"
  38.          BeginProperty Font 
  39.             Name            =   "MS Sans Serif"
  40.             Size            =   18
  41.             Charset         =   0
  42.             Weight          =   700
  43.             Underline       =   0   'False
  44.             Italic          =   0   'False
  45.             Strikethrough   =   0   'False
  46.          EndProperty
  47.          Height          =   1332
  48.          Index           =   6
  49.          Left            =   1320
  50.          TabIndex        =   16
  51.          Top             =   2760
  52.          Width           =   1332
  53.       End
  54.       Begin VB.CommandButton Command1 
  55.          Caption         =   "6"
  56.          BeginProperty Font 
  57.             Name            =   "MS Sans Serif"
  58.             Size            =   18
  59.             Charset         =   0
  60.             Weight          =   700
  61.             Underline       =   0   'False
  62.             Italic          =   0   'False
  63.             Strikethrough   =   0   'False
  64.          EndProperty
  65.          Height          =   1332
  66.          Index           =   5
  67.          Left            =   0
  68.          TabIndex        =   15
  69.          Top             =   2760
  70.          Width           =   1332
  71.       End
  72.       Begin VB.CommandButton Command1 
  73.          Caption         =   "5"
  74.          BeginProperty Font 
  75.             Name            =   "MS Sans Serif"
  76.             Size            =   18
  77.             Charset         =   0
  78.             Weight          =   700
  79.             Underline       =   0   'False
  80.             Italic          =   0   'False
  81.             Strikethrough   =   0   'False
  82.          EndProperty
  83.          Height          =   1332
  84.          Index           =   4
  85.          Left            =   2640
  86.          TabIndex        =   14
  87.          Top             =   1440
  88.          Width           =   1332
  89.       End
  90.       Begin VB.CommandButton Command1 
  91.          Caption         =   "4"
  92.          BeginProperty Font 
  93.             Name            =   "MS Sans Serif"
  94.             Size            =   18
  95.             Charset         =   0
  96.             Weight          =   700
  97.             Underline       =   0   'False
  98.             Italic          =   0   'False
  99.             Strikethrough   =   0   'False
  100.          EndProperty
  101.          Height          =   1332
  102.          Index           =   3
  103.          Left            =   1320
  104.          TabIndex        =   13
  105.          Top             =   1440
  106.          Width           =   1332
  107.       End
  108.       Begin VB.CommandButton Command1 
  109.          Caption         =   "3"
  110.          BeginProperty Font 
  111.             Name            =   "MS Sans Serif"
  112.             Size            =   18
  113.             Charset         =   0
  114.             Weight          =   700
  115.             Underline       =   0   'False
  116.             Italic          =   0   'False
  117.             Strikethrough   =   0   'False
  118.          EndProperty
  119.          Height          =   1332
  120.          Index           =   2
  121.          Left            =   0
  122.          TabIndex        =   12
  123.          Top             =   1440
  124.          Width           =   1332
  125.       End
  126.       Begin VB.CommandButton Command1 
  127.          Caption         =   "2"
  128.          BeginProperty Font 
  129.             Name            =   "MS Sans Serif"
  130.             Size            =   18
  131.             Charset         =   0
  132.             Weight          =   700
  133.             Underline       =   0   'False
  134.             Italic          =   0   'False
  135.             Strikethrough   =   0   'False
  136.          EndProperty
  137.          Height          =   1332
  138.          Index           =   1
  139.          Left            =   2640
  140.          TabIndex        =   11
  141.          Top             =   120
  142.          Width           =   1332
  143.       End
  144.       Begin VB.CommandButton Command1 
  145.          Caption         =   "1"
  146.          BeginProperty Font 
  147.             Name            =   "MS Sans Serif"
  148.             Size            =   18
  149.             Charset         =   0
  150.             Weight          =   700
  151.             Underline       =   0   'False
  152.             Italic          =   0   'False
  153.             Strikethrough   =   0   'False
  154.          EndProperty
  155.          Height          =   1332
  156.          Index           =   0
  157.          Left            =   1320
  158.          TabIndex        =   10
  159.          Top             =   120
  160.          Width           =   1332
  161.       End
  162.    End
  163.    Begin VB.TextBox numbertext 
  164.       Height          =   372
  165.       Left            =   4920
  166.       TabIndex        =   5
  167.       Top             =   6600
  168.       Width           =   1332
  169.    End
  170.    Begin VB.TextBox depthtext 
  171.       Height          =   372
  172.       Left            =   840
  173.       TabIndex        =   4
  174.       Top             =   6600
  175.       Width           =   1212
  176.    End
  177.    Begin VB.CommandButton Command3 
  178.       Caption         =   "&Manhatten"
  179.       BeginProperty Font 
  180.          Name            =   "MS Sans Serif"
  181.          Size            =   9.6
  182.          Charset         =   0
  183.          Weight          =   700
  184.          Underline       =   0   'False
  185.          Italic          =   0   'False
  186.          Strikethrough   =   0   'False
  187.       EndProperty
  188.       Height          =   612
  189.       Left            =   5760
  190.       TabIndex        =   3
  191.       ToolTipText     =   "Use Manhatten heuristics to solve (fast and doesn't consume much memory)"
  192.       Top             =   2400
  193.       Width           =   1692
  194.    End
  195.    Begin VB.CommandButton Command2 
  196.       Caption         =   "&New game"
  197.       BeginProperty Font 
  198.          Name            =   "MS Sans Serif"
  199.          Size            =   9.6
  200.          Charset         =   0
  201.          Weight          =   700
  202.          Underline       =   0   'False
  203.          Italic          =   0   'False
  204.          Strikethrough   =   0   'False
  205.       EndProperty
  206.       Height          =   612
  207.       Left            =   5760
  208.       TabIndex        =   2
  209.       ToolTipText     =   "Reset for new game"
  210.       Top             =   3600
  211.       Width           =   1692
  212.    End
  213.    Begin VB.CommandButton ButtonExit 
  214.       Caption         =   "&Exit"
  215.       BeginProperty Font 
  216.          Name            =   "MS Sans Serif"
  217.          Size            =   9.6
  218.          Charset         =   0
  219.          Weight          =   700
  220.          Underline       =   0   'False
  221.          Italic          =   0   'False
  222.          Strikethrough   =   0   'False
  223.       EndProperty
  224.       Height          =   612
  225.       Left            =   5760
  226.       TabIndex        =   1
  227.       ToolTipText     =   "Terminate the program"
  228.       Top             =   4680
  229.       Width           =   1692
  230.    End
  231.    Begin VB.CommandButton ButtonSolve 
  232.       Caption         =   "&BFS"
  233.       BeginProperty Font 
  234.          Name            =   "MS Sans Serif"
  235.          Size            =   9.6
  236.          Charset         =   0
  237.          Weight          =   700
  238.          Underline       =   0   'False
  239.          Italic          =   0   'False
  240.          Strikethrough   =   0   'False
  241.       EndProperty
  242.       Height          =   612
  243.       Left            =   5760
  244.       TabIndex        =   0
  245.       ToolTipText     =   "Use Breadth First Search to solve (memory hungry)"
  246.       Top             =   1320
  247.       Width           =   1692
  248.    End
  249.    Begin VB.Label Label3 
  250.       Alignment       =   2  'Center
  251.       Caption         =   "Bravo !!!"
  252.       BeginProperty Font 
  253.          Name            =   "MS Sans Serif"
  254.          Size            =   18
  255.          Charset         =   0
  256.          Weight          =   700
  257.          Underline       =   0   'False
  258.          Italic          =   0   'False
  259.          Strikethrough   =   0   'False
  260.       EndProperty
  261.       Height          =   732
  262.       Left            =   1320
  263.       TabIndex        =   8
  264.       Top             =   360
  265.       Visible         =   0   'False
  266.       Width           =   3972
  267.    End
  268.    Begin VB.Label Label2 
  269.       Caption         =   "Number of states in memory"
  270.       Height          =   372
  271.       Left            =   6240
  272.       TabIndex        =   7
  273.       Top             =   6600
  274.       Width           =   1932
  275.    End
  276.    Begin VB.Label Label1 
  277.       Caption         =   "Current depth of search"
  278.       Height          =   372
  279.       Left            =   2040
  280.       TabIndex        =   6
  281.       Top             =   6600
  282.       Width           =   1812
  283.    End
  284. Attribute VB_Name = "Puzzle"
  285. Attribute VB_GlobalNameSpace = False
  286. Attribute VB_Creatable = False
  287. Attribute VB_PredeclaredId = True
  288. Attribute VB_Exposed = False
  289. Option Explicit
  290. Dim arr() As New Stateposition        ' Global array of states
  291. Public numberstates As Long           ' Keeps track of size of arr() to
  292.                                       ' increase it if required
  293. Private Globalleft As Integer          ' Variables used to allow user moving
  294. Private Globaltop As Integer           ' tiles while "messing up" the state
  295. Private templeft As Integer
  296. Private temptop As Integer
  297. Private emptyx As Integer
  298. Private emptyy As Integer
  299. Dim position(3, 3) As Integer         ' 2-dimensional array to hold initial
  300.                                       ' position
  301. Private flag As Boolean                   ' reset flag for a new game
  302. Public finished As Boolean            ' indicates that game is finished
  303. Private index As Long                 ' index used in BFS
  304. Private redrawindex As Integer        ' index used while drawing a solution
  305. Private manhatten As Boolean        ' indicates what kind of search is used
  306. Dim solutionpath() As New Stateposition    ' array containing states of
  307.                                            ' the solution
  308.                                            
  309. Dim filled As Long         ' number of states in the array
  310. '****************************************************************
  311. '           TERMINATING THE GAME
  312. '****************************************************************
  313. Private Sub ButtonExit_Click()
  314.  Unload Me
  315. End Sub
  316. '****************************************************************
  317. '      SUB THAT TRIGGERS START OF THE SOLUTION. USES GLOBAL "MANHATTEN"
  318. '      FLAG TO SOLVE USING CORRESPONDING SOLUTION METHOD
  319. '****************************************************************
  320. Private Sub ButtonSolve_Click()
  321.  Dim i As Integer
  322.  If Not flag Then
  323.     ReDim arr(numberstates)          ' initializes array of states
  324.     numberstates = numberstates + 20
  325.     flag = True                      ' indicates that solving in process
  326.                                      ' blocking other requests
  327.     For i = 0 To 7                   ' "disables " all tiles
  328.       Command1(i).Enabled = False
  329.     Next
  330.     MousePointer = vbHourglass
  331.     subEnableButtons False
  332.     If Not manhatten Then            ' triggers BFS
  333.       startsolving
  334.     Else
  335.       manhattensolving               ' triggers Manhatten method
  336.     End If
  337.   End If
  338. End Sub
  339. '********************************************************************
  340. '            STANDARD METHOD THAT ALLOWS USER TO MOVE THE TILES. WORKS
  341. '            TOGETHER WITH DRAGDROP METHOD.
  342. '********************************************************************
  343. Private Sub Command1_MouseDown(index As Integer, Button As Integer, Shift As Integer, x As Single, y As Single)
  344.  templeft = Command1(index).Left
  345.  temptop = Command1(index).Top
  346.  Command1(index).Drag vbBeginDrag  ' moves corresponding tile
  347. End Sub
  348. '**************************************************************************
  349. '            CHANGES THE STATE OF COMMAND BUTTONS
  350. '**************************************************************************
  351. Private Sub subEnableButtons(bolValue As Boolean)
  352.  Command2.Enabled = bolValue
  353.  Command3.Enabled = bolValue
  354.  ButtonSolve.Enabled = bolValue
  355. End Sub
  356. '**********************************************************************
  357. '            CLICKING "RESET" BUTTON. FIRST OF ALL CLEANS ALL MEMORY
  358. '            LEFT FROM PREVIOUS GAME AND THEN RESETS INITIAL STATE.
  359. '**********************************************************************
  360. Private Sub Command2_Click()
  361.  Dim i As Long
  362.  MsgBox "Please wait, to allow memory deallocation"
  363.  If Not manhatten Then     ' BFS clean up
  364.    For i = 0 To index      ' runs on array of states
  365.      arr(i).freechild      ' deletes references to children and parents
  366.      Set arr(i) = Nothing
  367.    Next
  368.  Else                      ' Manhatten clean up
  369.    For i = 0 To filled
  370.     arr(i).freechild
  371.     Set arr(i).nextstate = Nothing
  372.     Set arr(i) = Nothing
  373.    Next i
  374.  End If
  375.  Unload Me               ' reloads the form
  376.  Load Me
  377.  Me.show
  378. End Sub
  379. '***********************************************************************
  380. '           TRIGGERS SOLUTION USING MANHATTEN METHOD.
  381. '***********************************************************************
  382. Private Sub Command3_Click()
  383.  manhatten = True
  384.  ButtonSolve_Click
  385. End Sub
  386. '***********************************************************************
  387. '      INITIALIZING GLOBAL VARIABLES OF THE FORM.
  388. '***********************************************************************
  389. Private Sub Form_Load()
  390.  Globalleft = 0
  391.  Globaltop = 120
  392.  numberstates = 20           ' initial length of states array
  393.  ReDim Preserve arr(numberstates)
  394.  emptyx = 0
  395.  emptyy = 0
  396.  flag = False                ' game didn't start
  397.  finished = False            ' game not finished
  398.  index = 0
  399.  manhatten = False           ' default
  400.  ReDim solutionpath(1)
  401.  position(0, 0) = 0          ' initial position
  402.  position(0, 1) = 1
  403.  position(0, 2) = 2
  404.  position(1, 0) = 3
  405.  position(1, 1) = 4
  406.  position(1, 2) = 5
  407.  position(2, 0) = 6
  408.  position(2, 1) = 7
  409.  position(2, 2) = 8
  410. End Sub
  411. '**********************************************************************
  412. '         SUB THAT ALLOWS MOVING AND DROPPING TILES USING MOUSE.
  413. '**********************************************************************
  414. Private Sub Frame1_DragDrop(Source As Control, x As Single, y As Single)
  415.  Dim xcoord As Integer
  416.  Dim ycoord As Integer
  417.  Source.Move Globalleft, Globaltop
  418.  Globalleft = templeft
  419.  Globaltop = temptop
  420.    For xcoord = 0 To 2
  421.      For ycoord = 0 To 2
  422.        If position(xcoord, ycoord) = Source.Caption Then  ' button that was moved
  423.          position(xcoord, ycoord) = 0
  424.          position(emptyx, emptyy) = Source.Caption
  425.          emptyx = xcoord                          ' update location of
  426.          emptyy = ycoord                          ' an empty slot
  427.          Exit Sub
  428.        End If
  429.      Next
  430.    Next
  431. End Sub
  432. '***********************************************************************
  433. '             SUB THAT INCREASES THE SIZE OF GLOBAL ARRAY OF STATES.
  434. '***********************************************************************
  435. Public Sub createstates()
  436.  ReDim Preserve arr(numberstates + 20)
  437.  numberstates = numberstates + 20
  438.  DoEvents
  439. End Sub
  440. '************************************************************************
  441. '          SUB THAT IMPLEMENTS BREADTH FIRST SEARCH. STATES ARE EXPANDED
  442. '          LEVEL BY LEVEL.
  443. '************************************************************************
  444. Private Sub startsolving()
  445.  Dim i As Integer
  446.  Dim temp As Stateposition
  447.  Dim x As Long
  448.  i = 0
  449.  filled = 1                 ' initially one state (current state) is
  450.                             ' in the array
  451.  Set arr(0) = New Stateposition
  452.  arr(0).onpath = True
  453.  arr(0).makearray position  ' creating initial position
  454.  Do While (Not finished)    ' until solution state is reached
  455.    arr(index).evaluate      ' evaluate the value of current state
  456.    depthtext.Text = arr(index).step + 1  ' update textfields with depth
  457.    depthtext.Refresh                     ' and number of states in memory
  458.    numbertext.Text = filled
  459.    numbertext.Refresh
  460.    If Not finished Then                  ' this is not solution state
  461.      
  462.       arr(index).expandchildren          ' expand it's children
  463.       
  464.       
  465.                                     ' increase the size of array if necessary
  466.       If ((filled + arr(index).numberchildren) > numberstates - 30) Then
  467.         createstates
  468.       End If
  469.       
  470.       arr(index).closed = True      ' close current state
  471.       
  472.       For i = 0 To arr(index).numberchildren    ' add children to array
  473.        Set arr(filled + i) = arr(index).getchild(i)
  474.       Next
  475.       
  476.       filled = filled + arr(index).numberchildren  ' update number of
  477.                                                ' states in array
  478.    Else
  479.      MousePointer = vbDefault
  480.                                 ' game is finished
  481.      If index = 0 Then
  482.       Label3.Visible = True     ' initial state was the solution one
  483.      Else
  484.       Set temp = New Stateposition
  485.       
  486.       Set temp = arr(index)     ' temp holds a solution state
  487.       
  488.       Do While Not temp.parent.onpath   ' mark all it's ancestors as
  489.         temp.onpath = True              ' a solution path
  490.         Set temp = temp.parent
  491.       Loop
  492.       temp.onpath = True
  493.       arr(0).onpath = False
  494.       
  495.       For x = 0 To 7             ' enable tile buttons
  496.        Command1(CInt(x)).Enabled = True
  497.       Next
  498.       
  499.       For redrawindex = 0 To index        ' iterate through an array
  500.         If (arr(redrawindex).onpath) Then ' and if state is on solution path
  501.           arr(redrawindex).redrawstate    ' redraw it
  502.         End If
  503.       Next
  504.       
  505.       Label3.Visible = True
  506.     End If
  507.    End If
  508.    index = index + 1           ' move to next unevaluated state in the array
  509.  Loop
  510. subEnableButtons True
  511. End Sub
  512. '***********************************************************************
  513. '             FUNCTION THAT TRANSLATES INDEX INTO CORRESPONDING
  514. '             X COORDINATE OF A TILE.
  515. '***********************************************************************
  516. Public Function translatex(x As Integer) As Integer
  517.  Select Case x
  518.   Case 0
  519.    translatex = 0
  520.   Case 1
  521.    translatex = 1320
  522.   Case 2
  523.    translatex = 2640
  524.  End Select
  525. End Function
  526. '***********************************************************************
  527. '             FUNCTION THAT TRANSLATES INDEX INTO CORRESPONDING
  528. '             Y COORDINATE OF A TILE.
  529. '***********************************************************************
  530. Public Function translatey(y As Integer) As Integer
  531.  Select Case y
  532.   Case 0
  533.    translatey = 120
  534.   Case 1
  535.    translatey = 1440
  536.   Case 2
  537.    translatey = 2760
  538.  End Select
  539. End Function
  540. '***********************************************************************
  541. '           FUNCTION THAT FINDS AND RETURNS INDEX OF THE CORRESPONDING
  542. '           TILE IN ARRAY OF BUTTONS ACCORDING TO PHYSICAL COORDINATES.
  543. '***********************************************************************
  544. Public Function findbutton(x As Integer, y As Integer)
  545.  Dim i As Integer
  546.   For i = 0 To 7
  547.    If (Command1(i).Left = x And Command1(i).Top = y) Then
  548.      findbutton = i
  549.    End If
  550.   Next
  551. End Function
  552. '*************************************************************************
  553. '                SUB THAT SOLVES PUZZLE USING MANHATTEN TECHNIQUE. NEXT
  554. '               STATE TO BE EVALUATED WILL ALWAYS BE THE "BEST" OF ALL
  555. '               UNEVALUATED SO FAR STATES.
  556. '*************************************************************************
  557. Public Sub manhattensolving()
  558.  Dim i As Integer
  559.  Dim x As Integer
  560.  Dim test As Stateposition
  561.  i = 0
  562.  filled = 1
  563.  Set arr(0) = New Stateposition
  564.  arr(0).onpath = True
  565.  Set test = New Stateposition
  566.  arr(0).makearray position
  567.  arr(0).evaluate
  568.  arr(0).onpath = True
  569.  arr(0).nextstate.curvalue = 100
  570.  Set test = arr(0)                ' test holds initial state
  571.    Do While (Not finished)        ' until the game is finished
  572.      test.expandchildren          ' expand children
  573.                                   
  574.                                   ' update textfields
  575.      depthtext.Text = test.step + 1
  576.      depthtext.Refresh
  577.      numbertext.Text = filled
  578.      numbertext.Refresh
  579.      
  580.                                   ' increase size of array if necessary
  581.      If ((filled + test.numberchildren) > numberstates - 30) Then
  582.        createstates
  583.      End If
  584.      
  585.      For i = 0 To test.numberchildren - 1   ' iterate through children
  586.      
  587.        Set arr(filled + i) = test.getchild(i)  ' insert them into the end of array
  588.        arr(filled + i).evaluate                ' evaluate child
  589.        rearrange test, arr(filled + i)         ' update reference to the
  590.                                                ' next best state
  591.        
  592.      Next
  593.      
  594.      filled = filled + test.numberchildren     ' update filled variable
  595.      Set test = test.nextstate                ' move to the next best state
  596.   Loop
  597.                                     
  598.   MousePointer = vbDefault
  599.                                   ' game is finished
  600.   If filled = 1 Then
  601.     Label3.Visible = True
  602.   Else
  603.    i = 1
  604.    Do While (Not test.parent.onpath)         ' iterate through ancestors of
  605.      ReDim Preserve solutionpath(i + 1)     ' the solution state and create
  606.      Set solutionpath(i) = test             ' array of states lying on the
  607.      test.onpath = True                     ' path
  608.      Set test = test.parent
  609.      i = i + 1
  610.    Loop
  611.      Set solutionpath(i) = test
  612.      For x = 0 To 7                        ' enable tile buttons
  613.       Command1(CInt(x)).Enabled = True
  614.      Next
  615.      Do While Not solutionpath(i).evaluate  ' iterate through solution path
  616.       solutionpath(i).redrawstate           ' redrawing state each time
  617.       i = i - 1
  618.      Loop
  619.      solutionpath(i).redrawstate
  620.      Label3.Visible = True
  621.    End If
  622. subEnableButtons True
  623. End Sub
  624. '************************************************************************
  625. '        SUB THAT CREATES ARRAY OF STATES LYING ON A SOLUTION PATH.
  626. '************************************************************************
  627. Public Sub setpath(temp As Stateposition)
  628.  Set solutionpath(0) = temp
  629. End Sub
  630. '************************************************************************
  631. '           SUB THAT INSERTS REFERENCE TO A NEW STATE INTO A CHAIN OF
  632. '           STATES ACCORDING TO ITS VALUE.
  633. '************************************************************************
  634. Private Sub rearrange(test As Stateposition, child As Stateposition)
  635.  Dim temp As New Stateposition
  636.  Dim swap As New Stateposition
  637.  Set temp = test
  638.    Do While (temp.nextstate.curvalue < child.curvalue)
  639.     Set temp = temp.nextstate            ' go further
  640.    Loop
  641.   Set swap = temp.nextstate           ' swap 2 references
  642.   Set temp.nextstate = child
  643.   Set child.nextstate = swap
  644. End Sub
  645.